<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Upload extends CI_Controller
{

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *         http://example.com/index.php/welcome
     *    - or -
     *         http://example.com/index.php/welcome/index
     *    - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see https://codeigniter.com/user_guide/general/urls.html
     */
    public function index()
    {

        $this->load->view('slide_upload');
    }

    public function md5File()
    {
        $file = 'C:\Users\admin\Downloads\GitKrakenSetup.exe';

        echo 'MD5 file hash of ' . $file . ': ' . md5_file($file);
    }

    public function convert()
    {
        phpinfo();exit;
        //$this->pdf2png("C:/slideloop.com/test.pdf","C:/slideloop.com/static");
        //$this->CMDImagick('C:/slideloop.com/test.pdf','C:/slideloop.com/static');
        //phpinfo();exit;
        $this->pdf2png("D:/slideloop.com/test.pdf", "D:/slideloop.com/static/");
    }

    protected function CMDImagick($pdf, $path)
    {
        if (!extension_loaded('imagick')) {
            return false;
        }

        if (!file_exists($pdf)) {
            echo 'Invalid File';
            return false;
        }

        //convert C:/slideloop.com/test.pdf C:/slideloop.com/static/test.pdf

        $md5FileName = md5(time());

        $pdfImageFileName = $path . '/' . $md5FileName . '.jpeg';

        $convertParams = 'convert ' . $pdf . ' ' . $pdfImageFileName;

        exec($convertParams, $log, $status);

        //print_r($log);

        //print_r($status);

        if ($status == 0) {
            return true;
        }

    }


    public function pdf2png($PDF, $Path)
    {
        if (!extension_loaded('imagick')) {
            return false;
        }
        if (!file_exists($PDF)) {
            return false;
        }

        if (!is_readable($PDF)) {
            echo 'file not readable';
        }
       
        /*
        $IM =new Imagick($PDF.'[0]');

        $IM->setResolution(120,120);
        $IM->setCompressionQuality(100);
        $IM->readImage($PDF);
         */
		$image = new Imagick();
			
        try {
           
            $image->setResolution(300, 300);
            var_dump($image->readImage('D:/slideloop.com/test.pdf'));
            $image->setImageCompressionQuality(90);
            foreach ($image as $Key => $Var) {
                $Var->setImageFormat('png');
                $fileName = $Path . md5($Key . time()) . '.png';
                if ($Var->writeImage($fileName) == true) {
                    $return[] = $fileName;
                }
			}
			
			$image->clear(); 
			$image->destroy();
            return $return;
        } catch (ImagickException $e) {
            echo "Error: " . $e->getMessage() . "\n";
        }
	}
    
	

}
